# -*- coding:utf-8 -*-
from typing import List

from django.contrib import admin
from .models import Apitest, Apistep, Apis
from django.utils.html import format_html  #将字符串格式化为html格式


class ApistepAdmin(admin.TabularInline):
    """
    @description:
    @param {type}
    @return:
    """
    list_display = [
        'apiname', 'apiurl', 'apiparamvalue', 'apimethod', 'apiresult',
        'apistatus', 'create_time', 'id', 'apitest'
    ]
    model = Apistep
    model_icon = 'fa fa-wifi'
    extra = 1


class ApitestAdmin(admin.ModelAdmin):
    """
    流程测试用例
    """
    def do_process(self, obj):
        """自定义一个标签，用于存放发送按钮"""
        dest = '/{}/send/'.format(obj.pk)
        title = '执行'
        return format_html("<a href='{}'>{}</a>".format(dest, title))

    search_fields = [
        'apitestname', 'apitester', 'apitestresult', 'create_time'
    ]
    list_filter = ['apitestname', 'apitestresult', 'create_time', 'id']
    list_display = [
        'apitestname', 'apitester', 'apitestresult', 'create_time', 'id',
        'do_process'
    ]
    inlines = [ApistepAdmin]
    model_icon = 'fa fa-cog'
    actions_on_bottom = True


class ApisAdmin(admin.ModelAdmin):
    """
    单一接口测试用例
    """
    def do_interface(self, obj):
        """
        自定义一个标签，用于存放发送按钮
        """
        Apis_list = list(Apis.objects.filter(id=obj.pk).values())
        # dest = '{}'.format(Apis_list.apiurl)
        dest = '/do_interface_case/{}'.format(obj.pk)
        title = '执行'
        print("我要干什么呢？", Apis_list)
        method = Apis_list[0]
        # btn= format_html('<a class="el-button el-button--primary"\
        # data-href-template="{}?&amp;_popup=1"  method="{}" title="用例执行"\
        # {}</a>'.format(dest, Apis_list.REQUEST_METHOD,  title))
        do_interface_case = format_html(
            "<a href={}  method={} name = 'do' >\
                        <span>{}</span></a>".format(dest, method['apimethod'],
                                                    title))
        # el-button el-button--primary
        # do_interface_case = btn
        return do_interface_case

    def do_inter(self, obj):
        Apis_list = Apis.objects.get(id=obj.pk)

        return True

    do_inter.short_description = '执行'
    actions = [
        do_inter,
    ]
    do_interface.short_description = "操作"
    search_fields = ['apiname', 'apiurl', 'apiparamvalue', 'apimethod']
    list_display = [
        'id', 'apiname', 'apiurl', 'apiparamvalue', 'apimethod', 'apiresult',
        'apistatus', 'create_time', 'Product', 'do_interface'
    ]
    model_icon = 'fa fa-comments'


# admin.site.register(Apitest, ApitestAdmin)  # 流程接口测试
# admin.site.register(Apis, ApisAdmin)  # 单一接口测试

admin.site.site_title = '海浪自动化测试平台'
admin.site.site_header = '海浪自动化测试平台'
